<?php
/******

	支付相关

*******/


namespace app\index\controller;

use think\Controller;
use app\index\model\User;
use app\index\model\Charge;
use think\Db;

class Recharge extends Base
{
	//生成订单号
	public function setorder(){
		$id= input('id'); 
		$sign = input('sign');//0普通vip1高级vip
        $status = $this->status;
        if($status == "error"){
            $dat = ['code' => 403, 'msg' => '登录超时,请重新登录'];
            return json_encode($dat);
        }
        else
        {

            $grade = user::where('id',$id)->value('grade');            
            if($grade == "2"){
              $dat = ['code'=>401,'msg'=>'您已经是高级VIP啦'];  
              return json_encode($dat,TRUE);

            }elseif($grade == "1" && $sign == "0"){
              $dat = ['code'=>402,'msg'=>'您已经是VIP啦，可以充值高级VIP'];  
              return json_encode($dat,TRUE);

            }elseif($grade == "1" &&  $sign == "1"){

                //充值高级vip
                $price = "999"; 
                $info = $this->testp($price,$sign);                
                $charge = new Charge;
                $time = date('Y-m-d:H:i:s');//当前时间
                $charge->data(['uid'=>$id,'order'=>$info['order'],'price'=>$info['price'],'created_at'=>$time]);
                $charge->save();
                $dat = ['code'=>200,'msg'=>'返回数据成功',$info];  
                return json_encode($dat,TRUE);

            }elseif($grade == "0" && $sign == "0"){
                //充值vip
                $price = "99";           
                $info = $this->testp($price,$sign);
                $charge = new Charge;
                $time = date('Y-m-d:H:i:s');//当前时间
                $charge->data(['uid'=>$id,'order'=>$info['order'],'price'=>$info['price'],'created_at'=>$time]);
                $charge->save();
                $dat = ['code'=>200,'msg'=>'返回数据成功',$info];  
                return json_encode($dat,TRUE);  

            }elseif($grade == "0" && $sign == "1"){
               //充值高级vip
                $price = "999";
                $info = $this->testp($price,$sign);
                $charge = new Charge;
                $time = date('Y-m-d:H:i:s');//当前时间
                $charge->data(['uid'=>$id,'order'=>$info['order'],'price'=>$info['price'],'created_at'=>$time]);
                $charge->save();

                $dat = ['code'=>200,'msg'=>'返回数据成功',$info];  
                return json_encode($dat,TRUE);
            }else{
          		$dat = ['code' => 403, 'msg' => '参数错误'];
          		return json_encode($dat);
          		die();
        	}       
        }
	}

	//数据监测
	public function testp($price,$sign){
		if($price == "99" && $sign == "0"){
			$vip = mt_rand(60,69);
			$build_order= $vip.date('Ymd').substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);
			 return $data = array("price"=>$price,"order"=>$build_order);
			
		}

		if($price == "999" && $sign == "1"){
			$svip = mt_rand(80,89);
			$build_order= $svip.date('Ymd').substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);
			 return $data = array("price"=>$price,"order"=>$build_order);
		
		}

	}



	//支付状态
	public function recharge(){
		$id = input('id');
		$order = input('order');
	  $data = input('data');
	    if($data == "succ"){          
            $ss = db('charge')->where('uid',$id)->where('order',$order)->setField('status','YES'); 
            $status = db('charge')->where('uid',$id)->where('order',$order)->field('status')->find();
            $infos =  db('charge')->where('uid',$id)->where('order',$order)->field('price')->find();
          if($status['status'] == "YES"){
           		if($infos['price'] == "99"){           		
                  $grade = "1";	                       
                  db('user')->where('id',$id)->setField('grade',$grade);     		 
                  $grade = db('user')->where('id',$id)->field('grade')->find();
                  $dat = ['code'=>200,'msg'=>'充值成功','sign'=>$grade];
                  return json_encode($dat,TRUE);
           		}elseif($infos['price'] == "999"){  
                  $grade = "2";         			  
           		    db('user')->where('id',$id)->setField('grade', $grade);
                  $grade = db('user')->where('id',$id)->field('grade')->find();
                  $dat = ['code'=>200,'msg'=>'充值成功','sign'=>$grade];
                  return json_encode($dat,TRUE);
               
           		}
           }elseif($status['status'] == "field"){ 
                 $dat = ['code'=>400,'msg'=>'充值失败'];
                 return json_encode($dat,TRUE);
           }
        }else{

	        // db('charge')->where('uid',$id)->where('order',$order)->delete();
            $dat = ['code'=>400,'msg'=>'充值失败'];
            return json_encode($dat,TRUE);
        }


	}

}